//
//  Voice_Bottom_View.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/23.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "Voice_Bottom_View.h"

@interface Voice_Bottom_View()
@property(nonatomic,strong) UIButton * record_item;
@property(nonatomic,strong) UIButton * record_edit_item;
@end
@implementation Voice_Bottom_View

#pragma mark - IBOutlet ActionEvents
-(void)transcribeEditEvent:(UIButton*)args{
    
}

-(void)transcribeStarting:(UIButton*)args{
    if(self.handler){
        self.handler(args);
    }
}
-(void)itemDidSelected:(UIButton*)args{
    args.selected = !args.selected;
    if(self.handler){
        self.handler(args);
    }
}
-(UIButton *)record_edit_item{
    _record_edit_item = ({
        if (!_record_edit_item) {
            _record_edit_item = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage * img = [UIImage imageNamed:@"qmx_bianji_tiezhi"];
            [_record_edit_item setImage:img forState:UIControlStateNormal];
            [_record_edit_item addTarget:self action:@selector(transcribeEditEvent:) forControlEvents:UIControlEventTouchUpInside];
        }
        _record_edit_item;
    });
    return _record_edit_item;
}

-(UIButton *)record_item{
    _record_item = ({
        if (!_record_item) {
            _record_item = [UIButton buttonWithType:UIButtonTypeCustom];
            _record_item.layer.masksToBounds = YES;
            _record_item.layer.borderWidth = 6.f;
            _record_item.layer.borderColor = [UIColor whiteColor].CGColor;
            _record_item.backgroundColor = UIColorFromRGB(0xFF4650);
            _record_item.layer.cornerRadius = 65/2.f;
            _record_item.tag = 1000;
            [_record_item addTarget:self action:@selector(transcribeStarting:) forControlEvents:UIControlEventTouchUpInside];
        }
        _record_item;
    });
    return _record_item;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp{
    __weak  typeof(self)weakSelf = self;
    self.backgroundColor = UIColorFromRGB(0x151724);
    [self addSubview:self.record_item];
    [self.record_item mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(65, 65));
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.mas_bottom).with.offset(-12.f);
    }];
    [self loadSubViews];
}
-(void)loadSubViews{
    __weak  typeof(self)weakSelf = self;
    [self addSubview:self.record_edit_item];
    [self.record_edit_item mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
     make.right.mas_equalTo(weakSelf.mas_right).with.offset(-12.f);
        make.top.mas_equalTo(weakSelf.mas_top).with.offset(12.f);
    }];
    __block NSMutableArray * containers = [NSMutableArray array];
    for (int i =0; i < 2; i++) {
        UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage * img =  (i == 0) ? [UIImage imageNamed:@"qmx_music_start"] : [UIImage imageNamed:@"qmx_detail_share"];
        item.tag = 1001 + i;
        if (i ==0) {
            UIImage * image = [UIImage imageNamed:@"qmx_voice_record_stop"];
            [item setImage:image forState:UIControlStateSelected];
        }
        [item setImage:img forState:UIControlStateNormal];
        [item addTarget:self action:@selector(itemDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        [containers addObject:item];
    }
    if (containers == nil) return;
    __block UIButton * anchor = nil;
    [containers enumerateObjectsUsingBlock:^(UIButton* item, NSUInteger idx, BOOL * _Nonnull stop) {
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(45, 45));
            make.centerY.mas_equalTo(weakSelf.record_item.mas_centerY).with.offset(0.f);
            if (anchor == nil) {
                make.centerX.mas_equalTo(weakSelf.mas_centerX).multipliedBy(0.5).offset(-45/2.f);
            }else{
                make.centerX.mas_equalTo(weakSelf.mas_centerX).multipliedBy(1.5).offset(45/2.f);
            }
            anchor = item;
        }];
    }];
}

@end
